/* Copyright ©, 2022, ECARX (Hubei) Technology Co., Ltd. All rights reserved.
 *
 * NOTICE: This file contains information which is confidential and proprietary to ECARX
 * (Hubei) Technology Co., Ltd. and/or other developers. No license to any intellectual
 * property rights contained within this file is either granted or implied except as may be
 * provided in an agreement with ECARX (Hubei) Technology Co., Ltd.
 *
 * Any unauthorized copying or distribution of content from this file in whole or in part
 * is prohibited.
 */
#ifndef ECARX_VHAL_CORE_UTILS_CONVERT_H
#define ECARX_VHAL_CORE_UTILS_CONVERT_H
#include <android/hardware/automotive/vehicle/2.0/types.h>
#include <string.h>

#include <cstdint>
#include <cstdio>
#include <type_traits>
#include <vector>
using namespace ::android::hardware::automotive::vehicle::V2_0;
namespace ecarx::vhal::core::utils {

/**
 * @brief 该函数只能用在proto序列化的对象上
 *
 * @tparam T
 * @tparam N
 * @param value
 * @return std::vector<uint8_t>
 */
template <typename T>
std::vector<uint8_t> toBytes(T&& protoObj) {
    std::vector<uint8_t> bytes(static_cast<size_t>(protoObj.ByteSize()));
    protoObj.SerializeToArray(bytes.data(), bytes.size());
    return bytes;
}

/**
 * @brief 该函数只能用在protobuff对象上
 *
 * @tparam T
 * @tparam N
 * @param data
 * @return T
 */
template <typename T>
T toValue(const std::vector<uint8_t>& bytes) {
    T protoObj;
    protoObj.ParseFromArray(bytes.data(), bytes.size());
    return protoObj;
}

}  // namespace swc::utils::convert

#endif